package com.dd.controller;


import com.dd.pojo.Commodity;
import com.dd.pojo.Vip;
import com.dd.service.CommodityService;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/comm")
public class CommodityController {
    @Autowired
    CommodityService commodityService;

    @RequestMapping("selectcomm")
    public String selectcomm() throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<Commodity> list = commodityService.selectcom();
        String str = mapper.writeValueAsString(list);
        String jso ="{\"code\":0,\"msg\":\"\",\"count\":" + 25 + ",\"data\":" + str + "}";
        return jso;
    }

    @RequestMapping("addcomm")
    public String addcomm(Commodity commodity){
        commodityService.addcom(commodity);
        return "1";
    }

    @RequestMapping("updatecommbyid")
    public String updatecommbyid(Commodity commodity){
        commodityService.updatecombyid(commodity);
        return "1";
    }

    @RequestMapping("deletecommbyid")
    public String deletecommbyid(int id){
        commodityService.deletecombyid(id);
        return "1";
    }

    @RequestMapping("/selectbycname")
    public String selectbykname(String cname) throws JsonProcessingException {
        ObjectMapper mapper = new ObjectMapper();
        List<Commodity> selectbycname = commodityService.selectbycname(cname);
        String str = mapper.writeValueAsString(selectbycname);
        String s ="{\"code\":0,\"msg\":\"\",\"count\":" + 25 + ",\"data\":" + str + "}";
        return s;
    }
}
